package weaver.interfaces.qz.job;

import com.engine.qz.service.impl.MeritsServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/11/12
 * @Description 人事系统自有数据计算
 * @Author AdminZm
 */
public class HrmOwnDataCountJob extends BaseCronJob {

    BaseBean baseBean = new BaseBean();

    @Override
    public void execute() {
        baseBean.writeLog("HrmOwnDataCountJob start.");
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        YearMonth ny = YearMonth.now();
        YearMonth sy = ny.minusMonths(1);
        List<String> yfList = new ArrayList<>();
        yfList.add(ny.toString());
        if (dayOfMonth <= 5) {
            yfList.add(sy.toString());
        }
        baseBean.writeLog("HrmOwnDataCountJob yfList:" + yfList);
        MeritsServiceImpl meritsServiceImpl = new MeritsServiceImpl();
        Map<String, Object> params = new HashMap<>();
        for (String yfTemp : yfList) {
            try {
                params = new HashMap<>();
                params.put("ny", yfTemp);
                Map<String, Object> map = meritsServiceImpl.hrmOwnDataCount(params);
                baseBean.writeLog("HrmOwnDataCountJob dt result:" + yfTemp + "、" + map);
            } catch (Exception e) {
                baseBean.writeLog("HrmOwnDataCountJob dt error:" + yfTemp + "、" + e);
            }
        }
        /*try {
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            RecordSet rs2 = new RecordSet();

            // 获取分割日
            String fgr = null;
            rs.execute("select fgr from uf_ywsjzdrwzxrqglb");
            if (rs.next()) {
                fgr = rs.getString("fgr");
            }
            if (StringUtils.isEmpty(fgr)) {
                fgr = "15";
            }

            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 获取本月的15号
            LocalDate fifteenth = currentDate.withDayOfMonth(Integer.parseInt(fgr));

            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter yearMonthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            Set<String> nySet = new HashSet<>();
            // 获取当前年月
            YearMonth currentYearMonth = YearMonth.now();
            String currentYearMonthStr = currentYearMonth.format(yearMonthFormatter);
            nySet.add(currentYearMonthStr);

            // 判断当前日期是否在15号之前还是之后
            if (currentDate.isBefore(fifteenth)) {
                // 获取上个月的年月
                YearMonth previousYearMonth = currentYearMonth.minusMonths(1);
                String previousYearMonthStr = previousYearMonth.format(yearMonthFormatter);
                nySet.add(previousYearMonthStr);
            }

            // 获取指定角色成员
            // 角色“DF_人资中心总”id
            String roleId = baseBean.getPropValue("qz_jx_zm_prop", "df_hrm_boss_role_id");
            Set<String> roleSet = new HashSet<>();
            rs.execute("SELECT RESOURCEID FROM hrmrolemembers WHERE ROLEID = " + roleId);
            while (rs.next()) {
                String id = rs.getString("RESOURCEID");
                if (StringUtils.isNotEmpty(id)) {
                    roleSet.add(id);
                }
            }

//            // 绩效偏离度指标id
//            String jxRuleId = baseBean.getPropValue("qz_jx_zm_prop", "jx_deviation_rule_id");
//            // 行政人员平均司龄指标id
//            String xzryRuleId = baseBean.getPropValue("qz_jx_zm_prop", "administration_avg_age_rule_id");
//            // 条线干部满编率指标id
//            String txgbRuleId = baseBean.getPropValue("qz_jx_zm_prop", "stripline_cadre_full_staffing_rule_id");
//            // 总部岗位offer数
//            String zzPostRuleId = baseBean.getPropValue("qz_jx_zm_prop", "zb_post_offer_cnt_rule_id");
//            // 顾问转正率（90天）指标id
//            String gwzzlRuleId = baseBean.getPropValue("qz_jx_zm_prop", "consultant_zz_rate_rule_id");
//            // 岗位fieldId 3
//            String gwFieldId = rs.getPropValue("qz_jx_zm_prop", "gw_field_id");
//            // 实习生用工性质id
//            String sxsUseKindId = rs.getPropValue("qz_jx_zm_prop", "sxs_usekind_id");
//            // offer发放流程表名
//            String offerTableName = rs.getPropValue("qz_jx_zm_prop", "offer_grant_table_name");
//            // 员工定薪流程表名
//            String salaryTableName = rs.getPropValue("qz_jx_zm_prop", "fixed_salary_table_name");
//            // M职级最低级
//            String mZjMinId = rs.getPropValue("qz_jx_zm_prop", "M_zj_min_id");
            // 转正日期fieldId 3
            String zzDateFieldId = rs.getPropValue("qz_jx_zm_prop", "zz_date_field_id");
            // 集团分部ids
            String groupFbIds = rs.getPropValue("qz_jx_zm_prop", "group_fb_ids");
            List<String> groupFbIdList = Arrays.asList(groupFbIds.split(","));

            List<List> insertParams = new ArrayList<>();
            // 人事系统自有业务数据建模id
            String modeId = rs.getPropValue("qz_jx_zm_prop", "hrm_own_mode_id");
            // 人事系统自有业务数据查询id
            String cusId = rs.getPropValue("qz_jx_zm_prop", "hrm_own_cus_id");

            String querySqlNew = " SELECT b.yg,b.szkhjh,c.szny, d.workcode FROM uf_ygmxzbdy_dt1 a, uf_ygmxzbdy b, uf_khjhdy c, hrmresource d " +
                    " WHERE a.mainid = b.id and b.szkhjh = c.id AND b.yg = d.id AND a.zbid = ? AND c.szny = ?";
            String querySqlTempNew = "SELECT id FROM uf_rsxtzyywsjb where yg = ? and ny = ? and zb = ?";
            String sql = "SELECT * FROM uf_rlzbjspzglb WHERE zt is null or zt != 0";
            rs.execute(sql);
            while (rs.next()) {
                String zb = rs.getString("zb");
                String fwsql = rs.getString("fwsql");
                String jszb = rs.getString("jszb");
                String ts = rs.getString("ts");
//                String sfbhbr = rs.getString("sfbhbr");
                if (StringUtils.isEmpty(zb) || StringUtils.isEmpty(fwsql) || StringUtils.isEmpty(jszb)) {
                    continue;
                }
                if (StringUtils.isEmpty(ts)) {
                    ts = "0";
                }
                // "净增人数"暂时不做; "绩效偏离度"不在这里处理
                if (StringUtils.equals(jszb, "4") || StringUtils.equals(jszb, "2")) {
                    continue;
                }
                for (String ny : nySet) {
                    Set<String> rySet = new HashSet<>();
                    Map<String, String> ghMap = new HashMap<>();
                    rs1.executeQuery(querySqlNew, zb, ny);
                    while (rs1.next()) {
                        String ygTemp = rs1.getString("yg");
                        String workcode = rs1.getString("workcode");
                        if (StringUtils.isEmpty(ygTemp) || StringUtils.isEmpty(workcode)) {
                            continue;
                        }
                        rySet.add(ygTemp);
                        ghMap.put(ygTemp, workcode);
                    }

                    if (CollectionUtils.isEmpty(rySet)) {
                        continue;
                    }
                    String resValueT = "0";

                    List<String> dataIdSet = new ArrayList<>();
                    rs1.execute(fwsql);
                    while (rs1.next()) {
                        String dataId = rs1.getString(1);
                        if (StringUtils.isEmpty(dataId)) {
                            continue;
                        }
                        dataIdSet.add(dataId);
                    }

                    if (CollectionUtils.isEmpty(dataIdSet)) {
                        continue;
                    }
                    int batchSize = 1000; // 每个小集合的大小
                    int size = dataIdSet.size(); // 原始集合的大小
                    int numberOfBatches = (int) Math.ceil((double) size / batchSize); // 划分的小集合数量
                    List<List<String>> dividedLists = new ArrayList<>(); // 存储划分后的小集合

                    for (int i = 0; i < numberOfBatches; i++) {
                        int fromIndex = i * batchSize;
                        int toIndex = Math.min((i + 1) * batchSize, size);
                        List<String> batch = dataIdSet.subList(fromIndex, toIndex);
                        dividedLists.add(batch);
                    }

                    // 干部满编率、满编率
                    if (StringUtils.equals(jszb, "0") || StringUtils.equals(jszb, "7")) {
                        String zbs = "0";
                        rs1.execute("select t1.id,t1.szfb,t1.bzfa ,t1.szbm,cast(t1.szgw  as varchar(1000))  jcljobtitle ,max(t1.bzs) bzs ,\n" +
                                "count(t5.id) zbs,max(t1.bzs)-count(t5.id) qbs,\n" +
                                "case when max(t1.bzs)-count(t5.id) >0 then '0' \n" +
                                "when max(t1.bzs)-count(t5.id) <0 then '2'\n" +
                                "else '1' end as QK\n" +
                                "from uf_bzqkhz t1\n" +
                                "left join uf_bzfagl t2 on t1.bzfa=t2.id\n" +
                                "left join uf_bzfagl_dt1 t3 on t2.id=t3.mainid\n" +
                                "left join hrmresource t4 on t1.szbm=t4.departmentid\n" +
                                "and t4.usekind in (select * from split(t3.yglx,','))\n" +
                                "and t4.status in (0,1,2,3)\n" +
                                "left join cus_fielddata t5 on t5.id=t4.id and t5.scopeid=3 and t5.field13 in\n" +
                                "(select * from split(t1.szgw,','))\n" +
                                "where (t2.bzwd= '0' or t2.bzwd='3')\n" +
                                "group by t1.szfb,t1.szbm,t1.id,t1.bzfa,cast(t1.szgw  as varchar(1000))");
                        while (rs1.next()) {
                            String idT = rs1.getString("id");
                            if (StringUtils.isEmpty(idT)) {
                                continue;
                            }
                            if (!dataIdSet.contains(idT)) {
                                continue;
                            }
                            String zbsT = rs1.getString("zbs");
                            if (StringUtils.isEmpty(zbsT)) {
                                zbsT = "0";
                            }
                            zbs = new BigDecimal(zbs).add(new BigDecimal(zbsT)).toString();
                        }

                        String bzs = "0";
                        for (List<String> batch : dividedLists) {
                            String ids = String.join(",", batch);
                            rs1.execute("SELECT id,bzs FROM uf_bzqkhz WHERE id in (" + ids + ")");
                            while (rs1.next()) {
                                String bzsT = rs1.getString("bzs");
                                if (StringUtils.isEmpty(bzsT)) {
                                    bzsT = "0";
                                }
                                bzs = new BigDecimal(bzs).add(new BigDecimal(bzsT)).toString();
                            }
                        }
                        if (new BigDecimal(bzs).compareTo(BigDecimal.ZERO) > 0) {
                            BigDecimal resValueBd = new BigDecimal(zbs).divide(new BigDecimal(bzs), 8, RoundingMode.DOWN);
                            if (resValueBd.compareTo(BigDecimal.ZERO) == 0) {
                                resValueT = "0";
                            } else {
                                resValueT = resValueBd.toString();
                            }
                        }
                    } else if (StringUtils.equals(jszb, "1")) {  // 顾问转正率
                        int zzrs = 0;
                        int yzzrs = 0;
                        YearMonth dqnyFormatter = YearMonth.parse(ny, yearMonthFormatter);
                        for (List<String> batch : dividedLists) {
                            String ids = String.join(",", batch);
                            rs1.execute("SELECT a.id,a.probationenddate,a.subcompanyid1,a.usekind,b." + zzDateFieldId + " as zzrq " +
                                    " FROM hrmresource a, cus_fielddata b where a.id = b.id and b.scope = 'HrmCustomFieldByInfoType' " +
                                    "and scopeid = 3 AND a.id in (" + ids + ")");
                            while (rs1.next()) {
                                String probationenddate = rs1.getString("probationenddate");
                                String zzrq = rs1.getString("zzrq");

                                if (StringUtils.isNotEmpty(zzrq)) {
                                    LocalDate date = LocalDate.parse(zzrq, formatter);
                                    YearMonth dateYearMonth = YearMonth.from(date);
                                    if (dqnyFormatter.equals(dateYearMonth)) {
                                        zzrs = zzrs + 1;
                                    }
                                }
                                if (StringUtils.isNotEmpty(probationenddate)) {
                                    LocalDate date = LocalDate.parse(probationenddate, formatter);
                                    YearMonth dateYearMonth = YearMonth.from(date);
                                    if (dqnyFormatter.equals(dateYearMonth)) {
                                        yzzrs = yzzrs + 1;
                                    }
                                }
                            }
                        }

                        if (yzzrs > 0) {
                            BigDecimal resValueBd = new BigDecimal(String.valueOf(zzrs)).divide(new BigDecimal(String.valueOf(yzzrs)), 8, RoundingMode.DOWN);
                            if (resValueBd.compareTo(BigDecimal.ZERO) == 0) {
                                resValueT = "0";
                            } else {
                                resValueT = resValueBd.toString();
                            }
                        }
                    } else if (StringUtils.equals(jszb, "2")) {  // 绩效偏离度

                    } else if (StringUtils.equals(jszb, "3")) {  // 离职人数
                        String firstDay = ny + "-01";
                        String lastDay = ny + "-31";
                        Set<String> lzSet = new HashSet<>();
                        rs1.execute("select resourceid from HRMSTATUSHISTORY  where type_n = 5 " +
                                "and changedate >= '" + firstDay + "' and changedate <= '" + lastDay + "'");
                        while (rs1.next()) {
                            String resourceid = rs1.getString("resourceid");
                            if (StringUtils.isNotEmpty(resourceid)) {
                                lzSet.add(resourceid);
                            }
                        }
                        if (!CollectionUtils.isEmpty(lzSet)) {
                            int cnt = 0;
                            for (String ry : dataIdSet) {
                                if (lzSet.contains(ry)) {
                                    cnt = cnt + 1;
                                }
                            }
                            resValueT = String.valueOf(cnt);
                        }
                    } else if (StringUtils.equals(jszb, "5")) {  // 离职率
                        String firstDay = ny + "-01";
                        String lastDay = ny + "-31";
                        Map<String, String> lzMap = new HashMap();
                        rs1.execute("select resourceid,changedate from HRMSTATUSHISTORY  where type_n = 5 " +
                                "and changedate <= '" + lastDay + "'");
                        while (rs1.next()) {
                            String resourceid = rs1.getString("resourceid");
                            String changedate = rs1.getString("changedate");
                            if (StringUtils.isNotEmpty(resourceid) && StringUtils.isNotEmpty(changedate)) {
                                lzMap.put(resourceid, changedate);
                            }
                        }
                        if (!CollectionUtils.isEmpty(lzMap)) {
                            int lzCnt = 0;
                            int zzCnt = 0;
                            for (String ry : dataIdSet) {
                                String lzrq = lzMap.get(ry);
                                if (StringUtils.isEmpty(lzrq)) {
                                    zzCnt = zzCnt + 1;
                                } else {
                                    if (!LocalDate.parse(lzrq, formatter).isBefore(LocalDate.parse(firstDay, formatter))) {
                                        lzCnt = lzCnt + 1;
                                        zzCnt = zzCnt + 1;
                                    }
                                }
                            }
                            if (lzCnt > 0 && zzCnt > 0) {
                                BigDecimal resValueBd = new BigDecimal(String.valueOf(lzCnt)).divide(new BigDecimal(String.valueOf(zzCnt)), 8, RoundingMode.DOWN);
                                if (resValueBd.compareTo(BigDecimal.ZERO) == 0) {
                                    resValueT = "0";
                                } else {
                                    resValueT = resValueBd.toString();
                                }
                            }
                        }
                    } else if (StringUtils.equals(jszb, "6")) {  // 分司新人留存率
                        YearMonth yearMonth = YearMonth.parse(ny, yearMonthFormatter);
                        // 获取该月的第一天
                        LocalDate firstDayOfMonth = yearMonth.atDay(1);
                        // 获取该月的最后一天
                        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
                        LocalDate startDateLd = firstDayOfMonth.minusDays(Integer.parseInt(ts));
                        LocalDate endDateLd = lastDayOfMonth.minusDays(Integer.parseInt(ts));
                        Set<String> lzSet = new HashSet<>();
                        rs1.execute("select resourceid from HRMSTATUSHISTORY  where type_n = 5 " +
                                "and changedate <= '" + lastDayOfMonth + "' and changedate >= '" + startDateLd + "'");
                        while (rs1.next()) {
                            String resourceid = rs1.getString("resourceid");
                            if (StringUtils.isNotEmpty(resourceid)) {
                                lzSet.add(resourceid);
                            }
                        }
                        int rzry = 0;
                        int lzry = 0;
                        rs1.execute("SELECT id FROM hrmresource WHERE companystartdate >= '" + startDateLd + "' " +
                                " AND companystartdate <= '" + endDateLd + "'");
                        while (rs1.next()) {
                            String id = rs1.getString("id");
                            if (StringUtils.isEmpty(id)) {
                                continue;
                            }
                            if (!dataIdSet.contains(id)) {
                                continue;
                            }
                            rzry = rzry + 1;
                            if ((!CollectionUtils.isEmpty(lzSet)) && lzSet.contains(id)) {
                                lzry = lzry + 1;
                            }
                        }
                        if (rzry > 0 && lzry >= 0 && rzry != lzry) {
                            int fz = rzry - lzry;
                            BigDecimal valueBd = new BigDecimal(String.valueOf(fz)).divide(new BigDecimal(rzry), 8, RoundingMode.DOWN);
                            if (valueBd.compareTo(BigDecimal.ZERO) != 0) {
                                resValueT = valueBd.toString();
                            }
                        }
                    }

                    for (String yg : rySet) {
                        String workcode = ghMap.get(yg);
                        if (StringUtils.isEmpty(workcode)) {
                            continue;
                        }
                        rs1.executeQuery(querySqlTempNew, yg, ny, zb);
                        int dataId = 0;
                        if (rs1.next()) {
                            dataId = rs1.getInt("id");
                        }
                        if (dataId > 0) {
                            rs1.executeUpdate("UPDATE uf_rsxtzyywsjb SET wcz = ?,modedatamodifydatetime = ? WHERE id= ?",
                                    resValueT, dtFormat.format(new Date()), dataId);
                        } else {
                            List insertParam = new ArrayList<>();
                            insertParam.add(yg);
                            insertParam.add(workcode);
                            insertParam.add(ny);
                            insertParam.add(zb);
                            insertParam.add(resValueT);
                            insertParam.add(modeId);
                            insertParam.add(cusId);
                            insertParam.add("1");
                            insertParam.add("0");
                            insertParam.add(sdfDate.format(new Date()));
                            insertParam.add(sdfTime.format(new Date()));
                            insertParams.add(insertParam);
                        }
                    }
                }
            }
//            for (String ny : nySet) {
//                // 行政人员平均司龄
//                String querySql = "SELECT t2.id,t2.usekind,t2.companyworkyear,t3.gwmc,t4.sxrq FROM " +
//                        " (SELECT a.id,a.companyworkyear,a.subcompanyid1,a.usekind,b." + gwFieldId + " as szgw " +
//                        " FROM hrmresource a, cus_fielddata b where a.id = b.id and " +
//                        " b.scope = 'HrmCustomFieldByInfoType' and scopeid = 3 AND a.status in (0,1,2,3)) t2 " +
//                        " left join uf_JCL_GWGL t3 on t2.szgw = t3.id " +
//                        " left join hrmsubcompany t4 on t2.subcompanyid1 = t4.id ";
//                String sumSl = "0";
//                String sumRs = "0";
//                rs.execute(querySql);
//                while (rs.next()){
//                    String ryId = rs.getString("id");
//                    String usekind = rs.getString("usekind");
//                    String companyworkyear = rs.getString("companyworkyear");
//                    String gwmc = rs.getString("gwmc");
//                    String sxrq = rs.getString("sxrq");
//                    if ((!CollectionUtils.isEmpty(roleSet)) && roleSet.contains(ryId)) {
//                        continue;
//                    }
//                    if (StringUtils.equals(usekind, sxsUseKindId)) {
//                        continue;
//                    }
//                    if (StringUtils.isEmpty(gwmc) || StringUtils.isEmpty(sxrq)) {
//                        continue;
//                    }
//                    if (gwmc.contains("行政")) {
//                        continue;
//                    }
//                    String nyTemp = sxrq.substring(0, 7);
//                    YearMonth yearMonthTemp2 = YearMonth.parse(nyTemp, yearMonthFormatter);
//                    YearMonth yearMonthTemp1 = YearMonth.parse(ny, yearMonthFormatter);
//                    long yearsDifference = yearMonthTemp1.getYear() - yearMonthTemp2.getYear();
//                    long monthsDifference = yearMonthTemp1.getMonthValue() - yearMonthTemp2.getMonthValue();
//                    if (monthsDifference < 0) {
//                        yearsDifference--;
//                        monthsDifference += 12;
//                    }
//
//                    // 计算总月份差异
//                    long totalMonthsDifference = yearsDifference * 12 + monthsDifference;
//                    if (totalMonthsDifference < 3) {
//                        continue;
//                    }
//                    if (StringUtils.isEmpty(companyworkyear)) {
//                        companyworkyear = "0";
//                    }
//                    sumSl = new BigDecimal(sumSl).add(new BigDecimal(companyworkyear)).toString();
//                    sumRs = new BigDecimal(sumRs).add(new BigDecimal("1")).toString();
//                }
//
//                String resValue = "0";
//                if (new BigDecimal(sumRs).compareTo(BigDecimal.ZERO) > 0) {
//                    resValue = (new BigDecimal(sumSl).multiply(new BigDecimal("12")))
//                            .divide(new BigDecimal(sumRs), 0, RoundingMode.DOWN).toString();
//                }
//                querySql = " SELECT a.*,b.yg,b.szkhjh,c.szny, d.workcode FROM uf_ygmxzbdy_dt1 a, uf_ygmxzbdy b, uf_khjhdy c, hrmresource d " +
//                        " WHERE a.mainid = b.id and b.szkhjh = c.id AND b.yg = d.id AND a.zbid = ? AND c.szny = ?";
//                String querySqlTemp = "SELECT id FROM uf_rsxtzyywsjb where yg = ? and ny = ? and zb = ?";
//                rs.executeQuery(querySql, xzryRuleId, ny);
//                while (rs.next()) {
//                    String yg = rs.getString("yg");
//                    String workcode = rs.getString("workcode");
//                    rs1.executeQuery(querySqlTemp, yg, ny, xzryRuleId);
//                    int dataId = 0;
//                    if (rs1.next()) {
//                        dataId = rs1.getInt("id");
//                    }
//                    if (dataId > 0) {
//                        rs1.executeUpdate("UPDATE uf_rsxtzyywsjb SET wcz = ?,modedatamodifydatetime = ? WHERE id= ?",
//                                resValue, dtFormat.format(new Date()), dataId);
//                    } else {
//                        List insertParam = new ArrayList<>();
//                        insertParam.add(yg);
//                        insertParam.add(workcode);
//                        insertParam.add(ny);
//                        insertParam.add(xzryRuleId);
//                        insertParam.add(resValue);
//                        insertParam.add(modeId);
//                        insertParam.add(cusId);
//                        insertParam.add("1");
//                        insertParam.add("0");
//                        insertParam.add(sdfDate.format(new Date()));
//                        insertParam.add(sdfTime.format(new Date()));
//                        insertParams.add(insertParam);
//                    }
//                }
//                // 绩效偏离度
//
//                // 条线干部满编率
//                String querySqlB = "SELECT * FROM uf_zbbkhrfwglb WHERE syzb = 0";
//                rs.execute(querySqlB);
//                Set<String> txmbSet = new HashSet<>();
//                while (rs.next()) {
//                    String bkhr = rs.getString("bkhr");
//                    String khfs = rs.getString("khfs");
//                    String khbm = rs.getString("khbm");
//                    String bzgwcj = rs.getString("bzgwcj");
//
//                    if (StringUtils.isEmpty(bkhr)) {
//                        continue;
//                    }
//                    String querySqlD = "SELECT id,bzs FROM uf_bzqkhz WHERE 1 = 1 ";
//                    if (StringUtils.isNotEmpty(bzgwcj)) {
//                        querySqlD = querySqlD + " and bzgwcj = " + bzgwcj;
//                    }
//                    if (StringUtils.isNotEmpty(khfs) && StringUtils.isNotEmpty(khbm)) {
//                        querySqlD = querySqlD + " AND (szfb in (" + khfs + ") OR szbm in (" + khbm + ")) ";
//                    } else if (StringUtils.isNotEmpty(khfs) && StringUtils.isEmpty(khbm)) {
//                        querySqlD = querySqlD + " AND szfb in (" + khfs + ") ";
//                    } else if (StringUtils.isEmpty(khfs) && StringUtils.isNotEmpty(khbm)) {
//                        querySqlD = querySqlD + " AND szbm in (" + khbm + ") ";
//                    }
//                    baseBean.writeLog("querySqlD is:" + querySqlD);
//
//                    String zbs = "0";
//                    String bzs = "0";
//                    rs1.execute(querySqlD);
//                    while (rs1.next()) {
//                        String id = rs1.getString("id");
//                        String bzsT = rs1.getString("bzs");
//                        if (StringUtils.isEmpty(bzsT)) {
//                            bzsT = "0";
//                        }
//                        bzs = new BigDecimal(bzs).add(new BigDecimal(bzsT)).toString();
//
//                        rs2.execute("select ,t1.szfb,t1.bzfa ,t1.szbm,cast(t1.szgw  as varchar(1000))  jcljobtitle ,max(t1.bzs) bzs ,count(t5.id) zbs,max(t1.bzs)-count(t5.id) qbs,\n" +
//                                " case when max(t1.bzs)-count(t5.id) >0 then '0' \n" +
//                                " when max(t1.bzs)-count(t5.id) <0 then '2'\n" +
//                                " else '1' end as QK\n" +
//                                " from uf_bzqkhz t1\n" +
//                                "left join uf_bzfagl t2 on t1.bzfa=t2.id\n" +
//                                "left join uf_bzfagl_dt1 t3 on t2.id=t3.mainid\n" +
//                                "left join hrmresource t4 on t1.szbm=t4.departmentid \n" +
//                                "\t\t\tand t4.usekind in (select * from split(t3.yglx,','))\n" +
//                                "\t\t\tand t4.status in (0,1,2,3)\n" +
//                                "left join cus_fielddata t5 on t5.id=t4.id and t5.scopeid=3 and t5.field13 in (select * from split(t1.szgw,','))\n" +
//                                "where (t2.bzwd= '0' or t2.bzwd='3') and t1.id = " + id +
//                                " group by t1.szfb,t1.szbm,t1.id,t1.bzfa,cast(t1.szgw  as varchar(1000))");
//                        while (rs2.next()) {
//                            String zbsT = rs2.getString("zbs");
//                            if (StringUtils.isEmpty(zbsT)) {
//                                zbsT = "0";
//                            }
//                            zbs = new BigDecimal(zbs).add(new BigDecimal(zbsT)).toString();
//                        }
//                    }
//                    String resValueT = "0";
//                    if (new BigDecimal(bzs).compareTo(BigDecimal.ZERO) > 0) {
//                        resValueT = new BigDecimal(zbs).divide(new BigDecimal(bzs), 2, RoundingMode.DOWN).toString();
//                    }
//                    String[] bkhrArr = bkhr.split(",");
//                    for (String bkhrSon : bkhrArr) {
//                        rs1.executeQuery(querySqlTemp, bkhrSon, ny, txgbRuleId);
//                        int dataId = 0;
//                        if (rs1.next()) {
//                            dataId = rs1.getInt("id");
//                        }
//                        if (dataId > 0) {
//                            rs1.executeUpdate("UPDATE uf_rsxtzyywsjb SET wcz = ?,modedatamodifydatetime = ? WHERE id= ?",
//                                    resValueT, dtFormat.format(new Date()), dataId);
//                        } else {
//                            if ((!CollectionUtils.isEmpty(txmbSet)) && txmbSet.contains(bkhrSon + "-" + ny)) {
//                                continue;
//                            }
//                            String workcode = null;
//                            rs1.execute("SELECT workcode FROM hrmresource WHERE id = " + bkhrSon);
//                            if (rs1.next()) {
//                                workcode = rs1.getString("workcode");
//                            }
//                            List insertParam = new ArrayList<>();
//                            insertParam.add(bkhrSon);
//                            insertParam.add(workcode);
//                            insertParam.add(ny);
//                            insertParam.add(txgbRuleId);
//                            insertParam.add(resValueT);
//                            insertParam.add(modeId);
//                            insertParam.add(cusId);
//                            insertParam.add("1");
//                            insertParam.add("0");
//                            insertParam.add(sdfDate.format(new Date()));
//                            insertParam.add(sdfTime.format(new Date()));
//                            insertParams.add(insertParam);
//                            txmbSet.add(bkhrSon + "-" + ny);
//                        }
//                    }
//                }
//
//                // 总部岗位offer数
//                querySql = " SELECT a.*,b.yg,b.szkhjh,c.szny, d.workcode FROM uf_ygmxzbdy_dt1 a, uf_ygmxzbdy b, uf_khjhdy c, hrmresource d " +
//                        " WHERE a.mainid = b.id and b.szkhjh = c.id AND b.yg = d.id AND a.zbid = ? AND c.szny = ?";
//                String querySqlA = "SELECT t1.*, t2.nczj from (SELECT rzgs, zj FROM " + salaryTableName + " WHERE requestid in (select dxlc from uf_RZGL where id in (select a.drzryid from " + offerTableName + " a, workflow_requestbase b WHERE a.requestId = b.requestId and " +
//                        "b.currentnodetype = 3 and a.jbr = ?))) t1 left join uf_nczj t2 on t1.zj = t2.id ";
//                rs.executeQuery(querySql, zzPostRuleId, ny);
//                while (rs.next()) {
//                    String yg = rs.getString("yg");
//                    String workcode = rs.getString("workcode");
//                    String resValueA = "0";
//                    rs1.executeQuery(querySqlA, yg);
//                    while (rs1.next()) {
//                        String rzgs = rs.getString("rzgs");
//                        String nczj = rs.getString("nczj");
//                        if (StringUtils.isEmpty(rzgs) || StringUtils.isEmpty(nczj)) {
//                            continue;
//                        }
//                        if (groupFbIdList.contains(rzgs)) {
//                            resValueA = new BigDecimal(resValueA).add(new BigDecimal("1")).toString();
//                        } else {
//                            if (nczj.startsWith("M") && nczj.length() > 1) {
//                                String s1 = nczj.substring(1);
//                                if (new BigDecimal(s1).compareTo(new BigDecimal(mZjMinId)) >= 0) {
//                                    resValueA = new BigDecimal(resValueA).add(new BigDecimal("1")).toString();
//                                } else {
//                                    resValueA = new BigDecimal(resValueA).add(new BigDecimal("0.5")).toString();
//                                }
//                            } else {
//                                resValueA = new BigDecimal(resValueA).add(new BigDecimal("0.5")).toString();
//                            }
//                        }
//                    }
//                    rs1.executeQuery(querySqlTemp, yg, ny, zzPostRuleId);
//                    int dataId = 0;
//                    if (rs1.next()) {
//                        dataId = rs1.getInt("id");
//                    }
//                    if (dataId > 0) {
//                        rs1.executeUpdate("UPDATE uf_rsxtzyywsjb SET wcz = ?,modedatamodifydatetime = ? WHERE id= ?",
//                                resValueA, dtFormat.format(new Date()), dataId);
//                    } else {
//                        List insertParam = new ArrayList<>();
//                        insertParam.add(yg);
//                        insertParam.add(workcode);
//                        insertParam.add(ny);
//                        insertParam.add(zzPostRuleId);
//                        insertParam.add(resValueA);
//                        insertParam.add(modeId);
//                        insertParam.add(cusId);
//                        insertParam.add("1");
//                        insertParam.add("0");
//                        insertParam.add(sdfDate.format(new Date()));
//                        insertParam.add(sdfTime.format(new Date()));
//                        insertParams.add(insertParam);
//                    }
//                }
//
//                // 顾问转正率（90天）
//                querySqlB = "SELECT * FROM uf_zbbkhrfwglb WHERE syzb = 1";
//                YearMonth dqnyFormatter = YearMonth.parse(ny, yearMonthFormatter);
//                Set<String> gwzzSet = new HashSet<>();
//                rs.execute(querySqlB);
//                while (rs.next()) {
//                    String bkhr = rs.getString("bkhr");
//                    String khfs = rs.getString("khfs");
//                    String khbm = rs.getString("khbm");
//                    String gwmc = rs.getString("gwmc");
//
//                    if (StringUtils.isEmpty(bkhr)) {
//                        continue;
//                    }
//                    String querySqlC = "SELECT a.probationenddate,b." + zzDateFieldId + " as zzrq from hrmresource a, cus_fielddata b, uf_JCL_GWGL c" +
//                            " where a.id = b.id and b." + gwFieldId + " = c.id and b.scope = 'HrmCustomFieldByInfoType' and scopeid = 3 AND a.status in (0,1,2,3) ";
//                    if (StringUtils.isNotEmpty(gwmc)) {
//                        querySqlC = querySqlC + "AND c.gwmc like '%" + gwmc + "%' ";
//                    }
//                    if (StringUtils.isNotEmpty(khfs) && StringUtils.isNotEmpty(khbm)) {
//                        querySqlC = querySqlC + " AND (a.subcompanyid1 in (" + khfs + ") OR a.departmentid in (" + khbm + ")) ";
//                    } else if (StringUtils.isNotEmpty(khfs) && StringUtils.isEmpty(khbm)) {
//                        querySqlC = querySqlC + " AND a.subcompanyid1 in (" + khfs + ") ";
//                    } else if (StringUtils.isEmpty(khfs) && StringUtils.isNotEmpty(khbm)) {
//                        querySqlC = querySqlC + " AND a.departmentid in (" + khbm + ") ";
//                    }
//                    baseBean.writeLog("querySqlC is:" + querySqlC);
//                    int yjzzry = 0;
//                    int zzry = 0;
//                    rs1.execute(querySqlC);
//                    while (rs1.next()) {
//                        String probationenddate = rs1.getString("probationenddate");
//                        String zzrq = rs1.getString("zzrq");
//
//                        if (StringUtils.isNotEmpty(zzrq)) {
//                            LocalDate date = LocalDate.parse(zzrq, formatter);
//                            YearMonth dateYearMonth = YearMonth.from(date);
//                            if (dqnyFormatter.equals(dateYearMonth)) {
//                                zzry = zzry + 1;
//                            }
//                        }
//                        if (StringUtils.isNotEmpty(probationenddate)) {
//                            LocalDate date = LocalDate.parse(probationenddate, formatter);
//                            YearMonth dateYearMonth = YearMonth.from(date);
//                            if (dqnyFormatter.equals(dateYearMonth)) {
//                                yjzzry = yjzzry + 1;
//                            }
//                        }
//                    }
//                    String resValueT = "0";
//                    if (yjzzry > 0) {
//                        resValueT = new BigDecimal(String.valueOf(zzry)).divide(new BigDecimal(String.valueOf(yjzzry)), 2, RoundingMode.DOWN).toString();
//                    }
//                    String[] bkhrArr = bkhr.split(",");
//                    for (String bkhrSon : bkhrArr) {
//                        rs1.executeQuery(querySqlTemp, bkhrSon, ny, gwzzlRuleId);
//                        int dataId = 0;
//                        if (rs1.next()) {
//                            dataId = rs1.getInt("id");
//                        }
//                        if (dataId > 0) {
//                            rs1.executeUpdate("UPDATE uf_rsxtzyywsjb SET wcz = ?,modedatamodifydatetime = ? WHERE id= ?",
//                                    resValueT, dtFormat.format(new Date()), dataId);
//                        } else {
//                            if ((!CollectionUtils.isEmpty(gwzzSet)) && gwzzSet.contains(bkhrSon + "-" + ny)) {
//                                continue;
//                            }
//                            String workcode = null;
//                            rs1.execute("SELECT workcode FROM hrmresource WHERE id = " + bkhrSon);
//                            if (rs1.next()) {
//                                workcode = rs1.getString("workcode");
//                            }
//                            List insertParam = new ArrayList<>();
//                            insertParam.add(bkhrSon);
//                            insertParam.add(workcode);
//                            insertParam.add(ny);
//                            insertParam.add(gwzzlRuleId);
//                            insertParam.add(resValueT);
//                            insertParam.add(modeId);
//                            insertParam.add(cusId);
//                            insertParam.add("1");
//                            insertParam.add("0");
//                            insertParam.add(sdfDate.format(new Date()));
//                            insertParam.add(sdfTime.format(new Date()));
//                            insertParams.add(insertParam);
//                            gwzzSet.add(bkhrSon + "-" + ny);
//                        }
//                    }
//                }
//            }

            if (CollectionUtils.isEmpty(insertParams)) {
                baseBean.writeLog("insertParams is null.");
            } else {
                baseBean.writeLog("insertParams size is:" + insertParams.size());
            }
            if (!CollectionUtils.isEmpty(insertParams)) {
                String insertSql = "INSERT INTO uf_rsxtzyywsjb (yg,gh,ny,zb,wcz,formmodeid, MODEUUID, modedatacreater," +
                        "modedatacreatertype, modedatacreatedate, modedatacreatetime) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
                rs.executeBatchSql(insertSql, insertParams);
            }
            // 权限重构
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            modeRightInfo.editModeDataShare(1, Integer.parseInt(modeId), Integer.parseInt(cusId));
        } catch (Exception e) {
            baseBean.writeLog("HrmOwnDataCountJob error:" + e);
        }*/
        baseBean.writeLog("HrmOwnDataCountJob end.");
    }
}
